package com.aaa.docter.service.Impl;

import com.aaa.docter.Fegin.UserFeign;
import com.aaa.docter.dao.DeptDao;
import com.aaa.docter.dao.RegistrationDao;
import com.aaa.docter.dao.SchedulingDao;
import com.aaa.docter.service.RegistrationService;
import com.aaa.docter.vo.DeptVo;
import com.aaa.docter.vo.RegistrationVo;
import com.aaa.entity.Dept;
import com.aaa.entity.HisRegistration;
import com.aaa.entity.Scheduling;
import com.aaa.util.JwtUtil;
import com.aaa.util.WebUtil;
import com.aaa.vo.Result;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.Registration;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

@Service
public class RegistrationServiceImpl implements RegistrationService {
    @Autowired
    private RegistrationDao registrationDao;
        @Autowired
    private SchedulingDao schedulingDao;

        @Autowired
        private UserFeign userFeign;


    @Override
    public Result<IPage<HisRegistration>> getAll(Integer currentPage, Integer pageSize, RegistrationVo registrationVo) {
        IPage<HisRegistration> iPage = new Page<>(currentPage, pageSize);
        QueryWrapper<HisRegistration> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(registrationVo.getDeptId())){
            wrapper.eq("dept_id",registrationVo.getDeptId());
        }

        if (StringUtils.hasText(registrationVo.getPatientName())){
            wrapper.like("patient_name",registrationVo.getPatientName());
        }
        if (StringUtils.hasText(registrationVo.getSchedulingType())){
            wrapper.eq("scheduling_type",registrationVo.getSchedulingType());
        }
        if (StringUtils.hasText(registrationVo.getSubsectionType())){
            wrapper.eq("subsection_type",registrationVo.getSubsectionType());
        }
        if (StringUtils.hasText(registrationVo.getRegistrationStatus())){
            wrapper.eq("registration_status",registrationVo.getRegistrationStatus());
        }
        if (Objects.nonNull(registrationVo.getCreateTime())){
            wrapper.eq("create_type",registrationVo.getCreateTime());
        }
        IPage<HisRegistration> registrationIPage = registrationDao.selectPage(iPage, wrapper);
        return new Result<>(2000,"查询挂号信息成功",registrationIPage);
    }

    @Override
    public Result<String> getTui(String registrationId) {
        Integer hisRegistration =registrationDao.UpdgetTui(registrationId);
        if (hisRegistration>0){
            return  Result.success();
        }else {
            return Result.error();
        }

    }

//    收费
    @Override
    public Result<String> getShoufei(String registrationId) {
        Integer hisRegistration =registrationDao.UpdgetShoufei(registrationId);
        if (hisRegistration>0){
            return  Result.success();
        }else {
            return Result.error();
        }
    }

    @Override
    public Result<String> getzuofei(String registrationId) {
        Integer hisRegistration =registrationDao.Updgetzuofei(registrationId);
        if (hisRegistration>0){
            return  Result.success();
        }else {
            return Result.error();
        }
    }

    @Override
    public Result<List<Scheduling>> getGuaHao(DeptVo deptVo) {
//        System.out.println("deptVo =-------------------- " + deptVo);
        QueryWrapper<Scheduling> wrapper = new QueryWrapper<>();
        //判断部门
        if (Objects.nonNull(deptVo.getDeptId())){
        wrapper.eq("dept_id",deptVo.getDeptId());
        }
        //挂号类型(门急诊)
       if (Objects.nonNull(deptVo.getSchedulingType())){
           wrapper.eq("scheduling_type",deptVo.getSchedulingType());
       }
        //挂号时段
        if (Objects.nonNull(deptVo.getSubsectionType())){
            wrapper.eq("subsection_type",deptVo.getSubsectionType());
        }
//        System.out.println("--------------------"+ JSON.toJSONString(deptVo.getSchedulingDay()));
        if (Objects.nonNull(deptVo.getSchedulingDay())){
            wrapper.eq("scheduling_day",deptVo.getSchedulingDay());
        }
        List<Scheduling> schedulings = schedulingDao.selectList(wrapper);
        return new Result<List<Scheduling>>(2000,"cg",schedulings);
    }

//查询挂号收费是否成功
    @Override
    public Result  getRegId(String registrationId) {
        QueryWrapper<HisRegistration> wrapper = new QueryWrapper<>();
        wrapper.eq("registration_id",registrationId);
        HisRegistration hisRegistration = registrationDao.selectOne(wrapper);
        if (hisRegistration.getRegistrationStatus().equals("1")){
            return Result.success().setMsg("收费成功");
        } else{
            return Result.success().setMsg("没收费");
        }

    }

//  新开就诊  待就诊
    @Override
    public Result<List<HisRegistration>> getschedulingType(String schedulingType) {
//      List<HisRegistration> getStatus= registrationDao.selectStatus(schedulingType);
//        return new Result<>(2000,"查询患者信息成功",getStatus);
        //直接在token获取手机号
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) JwtUtil.getTokenChaim(request.getHeader("token")).get("username");
        Long userId = userFeign.queryUserByPhone(Long.valueOf(phone)).getUserId();

        QueryWrapper<HisRegistration> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("scheduling_type",schedulingType);
        wrapper.eq("registration_status","1");
        List<HisRegistration> registrations = registrationDao.selectList(wrapper);
        return new Result<>(2000,"查询就诊中信息成功",registrations);
    }
    //  新开就诊  就诊ing
    @Override
    public Result<List<HisRegistration>> queryVisitingRegistration(String schedulingType) {
//        List<HisRegistration> getStatus2= registrationDao.selectStatus2(schedulingType);
//        return new Result<>(2000,"查询患者信息成功",getStatus2);
        HttpServletRequest request = WebUtil.getRequest();
        String phone = (String) JwtUtil.getTokenChaim(request.getHeader("token")).get("username");
        Long userId = userFeign.queryUserByPhone(Long.valueOf(phone)).getUserId();

        QueryWrapper<HisRegistration> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.eq("scheduling_type",schedulingType);
        wrapper.eq("registration_status","2");
        List<HisRegistration> registrations = registrationDao.selectList(wrapper);
        return new Result<>(2000,"查询就诊中信息成功",registrations);
    }
//    queryVisitCompletedRegistration
//    就诊完成
@Override
public Result<List<HisRegistration>> queryVisitCompletedRegistration(String schedulingType) {
//    List<HisRegistration> getStatus3= registrationDao.selectStatus3(schedulingType);
//    System.out.println("getStatus3................................. = " + getStatus3);
//    return new Result<>(2000,"查询患者信息成功",getStatus3);
    HttpServletRequest request = WebUtil.getRequest();
    String phone = (String) JwtUtil.getTokenChaim(request.getHeader("token")).get("username");
    Long userId = userFeign.queryUserByPhone(Long.valueOf(phone)).getUserId();

    QueryWrapper<HisRegistration> wrapper = new QueryWrapper<>();
    wrapper.eq("user_id",userId);
    wrapper.eq("scheduling_type",schedulingType);
    wrapper.eq("registration_status","3");
    List<HisRegistration> registrations = registrationDao.selectList(wrapper);
    return new Result<>(2000,"查询就诊中信息成功",registrations);
}

    @Override
    public Result<String> jiezhen(String registrationId) {
      Integer jiezhen= registrationDao.jiezhen(registrationId);
      if (jiezhen>0){
          return Result.success();
      }else {
          return Result.error();
      }

    }

//    完成就诊
    @Override
    public Result<String> wcjz(String registrationId) {

        Integer wcjz= registrationDao.Upwcjz(registrationId);
        if (wcjz>0){
            return Result.success();
        }else {
            return Result.error();
        }
    }

    @Override
    public Integer getRagNumber(Long deptId) {
        Integer regNumber = registrationDao.getRegNumber(deptId);
        System.out.println("regNumb=============er = " + regNumber);
//                registrationDao.getRegNumber(deptId) + 1;

        return regNumber;
    }


}
